package com.rslai.base.tool.servertest.intercept;

import com.rslai.base.tool.servertest.command.ParameterizedCommand;
import com.rslai.base.tool.servertest.command.StepCommand;
import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.model.KeyValueStore;
import com.rslai.base.tool.servertest.response.Response;
import com.rslai.base.tool.servertest.util.ParameterUtils;
import com.rslai.base.tool.servertest.util.ReflectionUtils;

import java.util.List;

public abstract class ParameterInterceptor implements StepCommandInterceptor {
    public Object beforeExecute(StepCommand command, Response preResult, Context context) {
        if (!(command instanceof ParameterizedCommand)) return preResult;
        List params = (List) ReflectionUtils.getValue(command, "params");
        if (!support(params)) return preResult;
        List processedParams = ParameterUtils.prepareParameters(params, preResult, context);
        processedParams = convert(processedParams);
        ReflectionUtils.setFieldValue(command, "params", processedParams);
        return preResult;
    }

    protected abstract List<KeyValueStore> convert(List<KeyValueStore> paramList);

    protected abstract boolean support(List<KeyValueStore> paramList);

    public Object afterExecute(StepCommand command, Response response, Context context) {
        return response;
    }
}
